package org.budo.mybatis.dao.statement.builder.creator;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;

/**
 * @author lmw
 */
public class UpdateColumnsStatementCreator extends AbstractStatementCreator {
    public UpdateColumnsStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_UPDATE_COLUMNS;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String idColumn = this.getStatementBuilder().getEntityConfiger().getIdColumn(entityType);

        String statementXml = _4 + "<update id=\"" + this.getStatementId() + "\" parameterType=\"map\">\n" //
                + _8 + "UPDATE " + tableName + "\n" //
                + _8 + "<set>\n" //
                + _12 + "<foreach item=\"key\" collection=\"columnsAndValues.keys\" separator=\",\">\n" //
                + _16 + "${key} = #{columnsAndValues.${key}}\n" //
                + _12 + "</foreach> \n"//
                + _8 + "</set>\n" //
                + _8 + " WHERE " + idColumn + " IN ( \n" //
                + _12 + "<foreach item=\"item\" collection=\"ids\" separator=\",\">\n" //
                + _16 + "#{item}\n" //
                + _12 + "</foreach>\n" //
                + _8 + " ) " + "\n"//
                + _4 + "</update>"; //
        return statementXml;
    }
}